package com.wf.ew.sell.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.sell.model.SellSon;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.sell.biz.EntryBillSonBiz;
import com.wf.ew.sell.model.EntryBillSon;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-11-24 00:09:57
 * @version ：1.0
 */
@Repository
public class EntryBillSonBizImpl extends BaseDao implements EntryBillSonBiz {

	@Override
	public String list(PageResult<T> page, EntryBillSon entryBillSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<EntryBillSon> rm = new BeanPropertyRowMapper<EntryBillSon>(EntryBillSon.class);

		StringBuffer sql = new StringBuffer("select e.id,e.companyId,e.warehouseName");
        sql.append(",e.productName,e.specification,e.lengths,e.negative,e.num");
		sql.append(",e.liTotalWeight,e.actualTotalWeight");
		sql.append(",e.liWeight,e.actualWeight,e.price,e.totalPrice,e.model");
		sql.append(",e.locality,e.createTime,e.createUser,e.updateTime");
		sql.append(",e.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_entry_bill_son e ");
        sql.append(" inner join sys_user suser on suser.id=e.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=e.companyId ");
        sql.append(" where e.del=0 and cmy.del=0 and e.entryBillId=:entryBillId ");
		map.put("entryBillId", entryBillSon.getEntryBillId());
    	if(Utils.isNotEmpty(entryBillSon)){
			if(Utils.isNotEmpty(entryBillSon.getWarehouseName())){
				sql.append(" and (instr(e.warehouseName,:warehouseName)>0 or instr(e.productName,:warehouseName)>0 " +
						" or instr(e.specification,:warehouseName)>0 ) ");
				map.put("warehouseName", entryBillSon.getWarehouseName());
			}
			if(Utils.isNotEmpty(entryBillSon.getSort())&&Utils.isNotEmpty(entryBillSon.getOrder())){
				sql.append(" order by "+entryBillSon.getSort()+" "+entryBillSon.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<EntryBillSon> list(EntryBillSon entryBillSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<EntryBillSon> rm = new BeanPropertyRowMapper<EntryBillSon>(EntryBillSon.class);

        StringBuffer sql = new StringBuffer("select e.id,e.companyId,e.warehouseName");
        sql.append(",e.productName,e.specification,e.lengths,e.negative,e.num");
		sql.append(",e.liTotalWeight,e.actualTotalWeight");
		sql.append(",e.liWeight,e.actualWeight,e.price,e.totalPrice,e.model");
		sql.append(",e.locality,e.createTime,e.createUser,e.updateTime");
		sql.append(",e.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_entry_bill_son e ");
        sql.append(" inner join sys_user suser on suser.id=e.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=e.companyId ");
		sql.append(" where e.del=0 and cmy.del=0 and e.entryBillId=:entryBillId ");
		map.put("entryBillId", entryBillSon.getEntryBillId());
    	if(Utils.isNotEmpty(entryBillSon)){
//			if(Utils.isNotEmpty(entryBillSon.getName())){
//				sql.append(" and instr(e.name,:name)>0 ");
//				map.put("name", entryBillSon.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Double calcTotalPrice(EntryBillSon entryBillSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
		StringBuffer sql = new StringBuffer("select IFNULL(sum(e.totalPrice),0) totalPrice ");
		sql.append(" from we_entry_bill_son e ");
		sql.append(" inner join we_company cmy on cmy.id=e.companyId ");
		sql.append(" where e.del=0 and cmy.del=0 and e.entryBillId=:entryBillId ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
			sql.append(" and e.companyId=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId());
		}
		map.put("entryBillId", entryBillSon.getEntryBillId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(),map,Double.class);
	}

	@Override
	public EntryBillSon getById(EntryBillSon entryBillSon) {
		RowMapper<EntryBillSon> rm = new BeanPropertyRowMapper<EntryBillSon>(EntryBillSon.class);
		Map<String, String> map  = new HashMap<String, String>();

		StringBuffer sql = new StringBuffer("select e.id,e.companyId,e.warehouseName");
        sql.append(",e.productName,e.specification,e.lengths,e.negative,e.num");
		sql.append(",e.liTotalWeight,e.actualTotalWeight");
		sql.append(",e.liWeight,e.actualWeight,e.price,e.totalPrice,e.model");
		sql.append(",e.locality,e.createTime,e.createUser,e.updateTime");
		sql.append(",e.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_entry_bill_son e ");
        sql.append(" inner join sys_user suser on suser.id=e.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=e.companyId ");
        sql.append(" where e.del=0 and cmy.del=0 and e.id=:id ");
		map.put("id", entryBillSon.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(EntryBillSon entryBillSon, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_entry_bill_son (");
        sql.append("entryBillId,companyId,warehouseName,productName,specification");
		sql.append(",lengths,negative,num,liWeight,actualWeight");
		sql.append(",liTotalWeight,actualTotalWeight");
		sql.append(",price,totalPrice,model,locality,createUser) values ( ");
        sql.append(":entryBillId,:companyId,:warehouseName,:productName,:specification");
		sql.append(",:lengths,:negative,:num,:liWeight,:actualWeight");
		sql.append(",:liTotalWeight,:actualTotalWeight");
		sql.append(",:price,:totalPrice,:model,:locality,:createUser)");
        map.put("entryBillId", entryBillSon.getEntryBillId());
        map.put("companyId", loginUser.getCompanyId());
        map.put("warehouseName", entryBillSon.getWarehouseName());
        map.put("productName", entryBillSon.getProductName());
        map.put("specification", entryBillSon.getSpecification());
        map.put("lengths", entryBillSon.getLengths());
        map.put("negative", entryBillSon.getNegative());
        map.put("num", entryBillSon.getNum()+"");
        map.put("liWeight", entryBillSon.getLiWeight());
        map.put("actualWeight", entryBillSon.getActualWeight());
        map.put("liTotalWeight", entryBillSon.getLiTotalWeight());
        map.put("actualTotalWeight", entryBillSon.getActualTotalWeight());
        map.put("price", entryBillSon.getPrice());
        map.put("totalPrice", entryBillSon.getTotalPrice());
        map.put("model", entryBillSon.getModel());
        map.put("locality", entryBillSon.getLocality());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(EntryBillSon entryBillSon, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_entry_bill_son set ");
        sql.append("warehouseName=:warehouseName,productName=:productName,specification=:specification ");
		sql.append(",lengths=:lengths,negative=:negative,num=:num,liWeight=:liWeight ");
		sql.append(",liTotalWeight=:liTotalWeight,actualTotalWeight=:actualTotalWeight ");
		sql.append(",actualWeight=:actualWeight,price=:price,totalPrice=:totalPrice,model=:model ");
		sql.append(",locality=:locality,updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("warehouseName", entryBillSon.getWarehouseName());
        map.put("productName", entryBillSon.getProductName());
        map.put("specification", entryBillSon.getSpecification());
        map.put("lengths", entryBillSon.getLengths());
        map.put("negative", entryBillSon.getNegative());
        map.put("num", entryBillSon.getNum()+"");
        map.put("liWeight", entryBillSon.getLiWeight());
        map.put("liTotalWeight", entryBillSon.getLiTotalWeight());
        map.put("actualTotalWeight", entryBillSon.getActualTotalWeight());
        map.put("actualWeight", entryBillSon.getActualWeight());
        map.put("price", entryBillSon.getPrice());
        map.put("totalPrice", entryBillSon.getTotalPrice());
        map.put("model", entryBillSon.getModel());
        map.put("locality", entryBillSon.getLocality());
		map.put("updateUser", loginUser.getId());
		map.put("id", entryBillSon.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(EntryBillSon entryBillSon, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(entryBillSon.getIds().length()>0){
			for(String ID : entryBillSon.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_entry_bill_son set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
